package com.gzc.just.play.last.war.sceneserver.gs;

import io.netty.channel.Channel;
import java.net.InetSocketAddress;

/**
 * GateServer信息类
 * 包含GateServer的连接信息和状态
 */
public class GateServerInfo {
    private int serverId;
    private Channel channel;
    private String password;
    private long lastPingTime;
    private boolean authenticated = false;

    /**
     * 默认构造函数
     */
    public GateServerInfo() {
        // 默认构造函数
    }

    /**
     * 带参数的构造函数
     * @param serverId 服务器ID
     * @param host 主机地址
     * @param port 端口号
     */
    public GateServerInfo(int serverId, String host, int port) {
        this.serverId = serverId;
        // 注意：这里需要根据实际情况添加host和port字段
    }

    public int getServerId() {
        return serverId;
    }

    public void setServerId(int serverId) {
        this.serverId = serverId;
    }

    public Channel getChannel() {
        return channel;
    }

    public void setChannel(Channel channel) {
        this.channel = channel;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public long getLastPingTime() {
        return lastPingTime;
    }

    public void setLastPingTime(long lastPingTime) {
        this.lastPingTime = lastPingTime;
    }

    public boolean isAuthenticated() {
        return authenticated;
    }

    public void setAuthenticated(boolean authenticated) {
        this.authenticated = authenticated;
    }

    public InetSocketAddress getRemoteAddress() {
        if (channel != null && channel.remoteAddress() instanceof InetSocketAddress) {
            return (InetSocketAddress) channel.remoteAddress();
        }
        return null;
    }

    public void updatePingTime() {
        this.lastPingTime = System.currentTimeMillis();
    }

    public boolean isTimeout(long timeoutMillis) {
        return System.currentTimeMillis() - lastPingTime > timeoutMillis;
    }

    public String getChannelId() {
        return channel != null ? channel.id().asLongText() : null;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        GateServerInfo other = (GateServerInfo) obj;
        return serverId == other.serverId;
    }
    
    @Override
    public int hashCode() {
        return Integer.hashCode(serverId);
    }
}
